﻿using System;
using System.Data;
using System.Data.SqlClient;
using DataAccessObject;
using SharedLibraries.Constants;
using ThangNM_LIB.FUNC;

namespace ThangNM.UserControl.Admin
{
    public partial class TSQLUC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Title_FormUC1.setTitle(ConstEx.Table_Name_C.TSQL, ConstEx.funcButtonToolBar.NA);
            MsgUC1.setClearMsg();
            if (!IsPostBack)
            {
                //if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                //{
                //    pnlMain.Visible = false;
                //    Commons.setAccessDenied(Page, MsgUC1);
                //    return;
                //}
            }
        }

        protected void btnExcute_Click(object sender, EventArgs e)
        {
            //MsgUC1.setNotifications(ConstEx.FunctionSetMsg.INFO, MessagesEx.CHUC_NANG_NAY_DANG_TAM_KHOA);
            //return;

            grvList.DataSource = null;
            grvList.DataBind();

            if (StringEx.CompareEx(txtQuery.Text, "tblAccount") || StringEx.CompareEx(txtQuery.Text, "EXEC") || StringEx.CompareEx(txtQuery.Text, "TRUNCATE") || StringEx.CompareEx(txtQuery.Text, "INSERT") || StringEx.CompareEx(txtQuery.Text, "UPDATE") || StringEx.CompareEx(txtQuery.Text, "DELETE") || StringEx.CompareEx(txtQuery.Text, "DROP") || StringEx.CompareEx(txtQuery.Text, "ALTER") || StringEx.CompareEx(txtQuery.Text, "CREATE"))
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Không được sử dụng các lệnh sau: EXEC, TRUNCATE, INSERT, UPDATE, DELETE, DROP, ALTER, CREATE");
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand(txtQuery.Text);
                DataTable dt = (new DB_SQL_EXT()).GetDataTable(cmd);
                grvList.DataSource = dt;
                grvList.DataBind();
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }
    }
}